/**
 *      Attribute:
 *          (   
 *              id int primary key AUTOINCREMENT,
 *              neme char(256),                         //种类名
 *              bgimg text                              //背景url
 *          )
 */

const { Sequelize, DataTypes } = require('sequelize')
const express = require('express')


function createModel(sequelize) {
    return sequelize.define('category', {
        id: { type: DataTypes.INTEGER, primaryKey: true },
        name: { type: DataTypes.STRING, allowNull: false, unique: true },
        bgimg: DataTypes.STRING
    }, {
        freezeTableName: true,
        tableName: 'category',
        timestamps: false
    })
}

let router = express.Router()
let categoryModel = undefined

/**
 * @swagger
 * /api/v2/category:
 *  post:
 *      summary:
 *      tags: [Category]
 *      description: 添加分类数据
 *      requestBody:
 *          required: true
 *          content:
 *              application/json:
 *                  schema:
 *                      type: object
 *                      required:
 *                          - name
 *                      properties:
 *                          name:
 *                              type: string
 *                          bgimg:
 *                              type: string            
 *      responses:
 *          200:
 *              description: successed
 *              content:
 *                  application/json:
 *                      schema:
 *                          type: object
 *                          properties:
 *                              result:
 *                                  type: string
 *                              desc:
 *                                  type: string
 *                              data:
 *                                  type: object
 *                                  properties:
 *                                      id:
 *                                          type: string
 *                                      name:
 *                                          type: string
 *                                      bgimg:
 *                                          type: string
 *                                          
 */
router.post('/category', (req, res)=>{
    console.log('receive json body ' + JSON.stringify(req.body))

    categoryModel.create(req.body)
                    .then((category)=>{
                        console.log('Add: ' + JSON.stringify(category))
                        res.status(200).json({result: 'success', desc: '', data: category})
                    })
                    .catch((err)=>{
                        res.status(500).json({result: 'fail', desc: err})
                    })
})

/**
 * @swagger
 * /api/v2/category:
 *      delete:
 *          summary:
 *          tags: [Category]
 *          description: 删除指定的分类数据
 *          parameters:
 *              - name: id
 *                in: query
 *                required: true
 *          responses:
 *              200:
 *                  description: deleted
 *                  content:
 *                      application/json:
 *                          schema:
 *                              type: object
 *                              properties:
 *                                  result:
 *                                      type: string
 *                                  desc:
 *                                      type: string
 */
router.delete('/category', (req, res)=>{
    categoryModel.destroy({ where: {id: req.query.id}})
                    .then((category)=>{
                        console.log('Delete: ' + JSON.stringify(category))
                        res.status(200).json({result: 'success', desc: ''})
                    })
                    .catch((err)=>{
                        res.status(500).json({result: 'fail', desc: err})
                    })
    
})

/**
 * @swagger
 * /api/v2/category:
 *      put:
 *          summary:
 *          tags: [Category]
 *          description: 修改指定的分类数据
 *          parameters:
 *              - name: id
 *                description: 要修改的category id
 *                in: query
 *                required: true
 *                type: string
 *          requestBody:
 *              required: true
 *              content:
 *                  application/json:
 *                  schema:
 *                      type: object
 *                      required:
 *                          - name
 *                      properties:
 *                          name:
 *                              type: string
 *                          bgimg:
 *                              type: string
 *          responses:
 *              200:
 *                  description: connect successed.
 *                  content:
 *                      application/json:
 *                          schema:
 *                              type: object
 *                              properties:
 *                                  result:
 *                                      type: string
 *                                  desc:
 *                                      type: string
 *  
 */
router.put('/category', (req, res)=>{
    console.log('json>>> ' + JSON.stringify(req.body))
    console.log('id>>> ' + req.query.id)
    categoryModel.update(req.body, { where: {id: req.query.id}})
                    .then((category)=>{
                        console.log('Update: ' + JSON.stringify(category))
                        res.status(200).json({result: 'success', desc: '' })
                    })
                    .catch((err)=>{
                        res.status(500).json({result: 'fail', desc: err })
                    })
})

/**
 * @swagger
 * /api/v2/category:
 *      get:
 *          tags: [Category]
 *          summary:
 *          description: 获取分类数据
 *          parameters:
 *              - name: id
 *                in: query
 *                required: false
 *                type: string
 *          responses:
 *              200:
 *                  description: successed
 *              500:
 *                  description: failed
 */
router.get('/category', (req, res)=>{
    let condition = { where: { id: req.query.id }}
    if(condition.where.id == undefined || condition.where.id == '') {
        delete condition.where
    }

    categoryModel.findAll(condition)
        .then((categorys)=>{
            console.log('Get: ' + JSON.stringify(categorys))
            res.status(200).json({result: 'success', desc: '', data: categorys })
        })
        .catch((err)=>{
            res.status(500).json({result: 'fail', desc: err })
        })
    
})


module.exports = {
    init: function(sequelize) {
        categoryModel = createModel(sequelize)
    },
    api: function() {
        return router
    }
}